{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
}}

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @mountName={{@backend}}>
  <:tabLinks>
    <li>
      <LinkTo
        @route={{this.router.currentRoute.localName}}
        @models={{@currentRouteParams}}
        data-test-secrets-tab="Secrets"
        @current-when={{true}}
      >Secrets</LinkTo>
    </li>
    <li>
      <LinkTo @route="configuration" @model={{@backend}} data-test-secrets-tab="Configuration">Configuration</LinkTo>
    </li>
  </:tabLinks>

  <:toolbarFilters>

    {{#if (and (not-eq @secrets 403) (or @secrets @filterValue))}}
      <KvListFilter @secrets={{@secrets}} @mountPoint={{this.mountPoint}} @filterValue={{@filterValue}} />
    {{/if}}
  </:toolbarFilters>

  <:toolbarActions>
    <ToolbarLink
      data-test-toolbar-create-secret
      @route="create"
      @model={{@backend}}
      @query={{hash initialKey=@filterValue}}
      @type="add"
    >
      Create secret
    </ToolbarLink>
  </:toolbarActions>
</KvPageHeader>
{{#if (eq @secrets 403)}}
  <div class="box is-fullwidth is-shadowless has-tall-padding">
    <div class="selectable-card-container one-card">
      <OverviewCard
        @cardTitle="View secret"
        @subText="Type the path of the secret you want to view. Include a trailing slash to navigate to the list view."
      >
        <:content>
          <form {{on "submit" this.transitionToSecretDetail}} class="has-top-margin-m">
            <InputSearch
              @id="search-input-kv-secret"
              @initialValue={{@pathToSecret}}
              @onChange={{this.handleSecretPathInput}}
              @placeholder="secret/"
              data-test-view-secret
            />
            <Hds::Button
              @text={{this.buttonText}}
              @color="secondary"
              type="submit"
              disabled={{not this.secretPath}}
              data-test-submit
            />
          </form>
          {{#if @failedDirectoryQuery}}
            <AlertInline
              @type="danger"
              @message="You do not have the required permissions or the directory does not exist."
            />
          {{/if}}
        </:content>
      </OverviewCard>
    </div>
  </div>
{{else}}
  {{#if @secrets}}
    {{#each @secrets as |metadata|}}
      <LinkedBlock
        data-test-list-item={{metadata.path}}
        class="list-item-row"
        @params={{array (if metadata.pathIsDirectory "list-directory" "secret.index") @backend metadata.fullSecretPath}}
        @linkPrefix={{this.mountPoint}}
      >
        <div class="level is-mobile">
          <div class="level-left">
            <div>
              <Icon @name={{if metadata.pathIsDirectory "folder" "file"}} class="has-text-grey-light" />
              <span class="has-text-weight-semibold is-underline" data-test-path>
                {{metadata.path}}
              </span>
            </div>
          </div>
          <div class="level-right is-flex is-paddingless is-marginless">
            <div class="level-item">
              <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
                <dd.ToggleIcon
                  @icon="more-horizontal"
                  @text="Manage secret"
                  @hasChevron={{false}}
                  data-test-popup-menu-trigger
                />
                {{#if metadata.pathIsDirectory}}
                  <dd.Interactive
                    @route="list-directory"
                    @models={{array @backend metadata.fullSecretPath}}
                  >Content</dd.Interactive>
                {{else}}
                  <dd.Interactive
                    @route="secret.index"
                    @models={{array @backend metadata.fullSecretPath}}
                  >Overview</dd.Interactive>
                  <dd.Interactive @route="secret.details" @models={{array @backend metadata.fullSecretPath}}>Secret data</dd.Interactive>
                  {{#if metadata.canReadMetadata}}
                    <dd.Interactive @route="secret.metadata.versions" @models={{array @backend metadata.fullSecretPath}}>
                      View version history</dd.Interactive>
                  {{/if}}
                  {{#if metadata.canDeleteMetadata}}
                    <dd.Interactive
                      @color="critical"
                      {{on "click" (fn (mut this.metadataToDelete) metadata)}}
                      data-test-popup-metadata-delete
                    >Permanently delete</dd.Interactive>
                  {{/if}}
                {{/if}}
              </Hds::Dropdown>
            </div>
          </div>
        </div>
      </LinkedBlock>
    {{/each}}
    {{#if this.metadataToDelete}}
      <ConfirmModal
        @color="critical"
        @onClose={{fn (mut this.metadataToDelete) null}}
        @onConfirm={{fn this.onDelete this.metadataToDelete}}
        @confirmMessage="This will permanently delete this secret and all its versions."
      />
    {{/if}}
    <Hds::Pagination::Numbered
      @currentPage={{@secrets.meta.currentPage}}
      @currentPageSize={{@secrets.meta.pageSize}}
      @route={{this.router.currentRoute.localName}}
      @showSizeSelector={{false}}
      @models={{@currentRouteParams}}
      @totalItems={{@secrets.meta.total}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  {{else}}
    {{#if @filterValue}}
      <EmptyState @title='There are no secrets matching "{{@filterValue}}".' />
    {{else}}
      <EmptyState
        data-test-secret-list-empty-state
        @title="No secrets yet"
        @message="When created, secrets will be listed here. Create a secret to get started."
      />
    {{/if}}
  {{/if}}
{{/if}}